// const log = require('./loglog');

class yzApi {
	constructor(page) {
		this.page = page;
	}
	// 模拟点击元素
	async clickBySelector(nodeName, selector, timeout, sleepTime) {
		try {
			const element = await this.page.waitForSelector(selector, { timeout });
			await element.click();
			console.log(`✅找到并模拟点击:${nodeName}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`没找到:${nodeName}`);
			return false;
		}
	}
	// 使用元素的click事件点击元素
	async clickByEvalSelector(nodeName, selector, timeout, sleepTime) {
		try {
			await this.page.waitForSelector(selector, { timeout });
			const element = await this.page.$eval(selector, (el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
				el.click();
			});
			console.log(`✅找到并点击:${nodeName}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`);
			return false;
		}
	}
	// 创建一个点击事件 在元素身上执行这个点击事件（类似于元素自带的click事件）
	async clickByMouseEventSelector(nodeName, selector, timeout, sleepTime) {
		try {
			const element = await this.page.waitForSelector(selector, { timeout });
			await this.page.evaluate((el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
				const event = new MouseEvent('click', {
					view: window,
					bubbles: true,
					cancelable: true,
				});
				el.dispatchEvent(event);
			}, element);
			console.log(`✅找到并点击:${nodeName}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`);
			return false;
		}
	}
	// 等待一个页面元素出现
	async findOneBySelector(nodeName, selector, timeout, sleepTime) {
		try {
			const element = await this.page.waitForSelector(selector, { timeout });

			await this.page.evaluate((el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
			}, element);
			console.log(`✅找到:${nodeName}`);
			await sleep(sleepTime || 1);
			return element;
		} catch (error) {
			console.log(`没找到:${nodeName}`);
			return false;
		}
	}
	// 获取元素数组 并拿到最后一个
	async findLastOneBySelector(nodeName, selector, timeout, sleepTime) {
		try {
			await this.page.waitForSelector(selector, { timeout });
			const elements = await this.page.$$(selector);
			if (elements.length > 0) {
				console.log(`✅找到并点击:${nodeName}`);
				await sleep(sleepTime || 1);
				return elements[elements.length - 1];
			} else {
				await sleep(sleepTime || 1);
				return false;
			}
		} catch (error) {
			console.log(`❌没找到:${nodeName}`);
			return false;
		}
	}
	// 获取元素数组
	async findArrBySelector(nodeName, selector, timeout, sleepTime) {
		try {
			await this.page.waitForSelector(selector, { timeout });
			const elements = await this.page.$$(selector);
			if (elements.length > 0) {
				console.log(`✅找到:${nodeName}`);
				await sleep(sleepTime || 1);
				return elements;
			} else {
				await sleep(sleepTime || 1);
				return false;
			}
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	// 找到单个元素输入循环输入内容
	async inputTypeBySelector(nodeName, text, selector, timeout, sleepTime) {
		try {
			const element = await this.page.waitForSelector(selector, { timeout });
			await element.type(text);
			console.log(`✅找到:${nodeName}.并输入`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(error);
			console.log(`❌没找到:${nodeName}`);
			return false;
		}
	}
	// 找到单个元素输入循环输入内容 --
	async inputTypeEnterBySelector(nodeName, texts, selector, timeout, sleepTime) {
		try {
			const element = await this.page.waitForSelector(selector, { timeout });
			await element.click();
			for (let index = 0; index < 100; index++) {
				this.page.keyboard.press('Backspace');
			}
			for (const te of texts) {
				await element.type(te);
				this.page.keyboard.down('Enter');
				this.page.keyboard.up('Enter');
			}
			console.log(`✅找到:${nodeName}.并输入:${texts}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`);
			return false;
		}
	}
	// 找到单个元素输入循环输入内容
	async inputWhileTypeBySelector(nodeName, text, selector, timeout, sleepTime) {
		try {
			let element;
			while (true) {
				element = await this.page.waitForSelector(selector, { timeout });
				await element.scrollIntoView();
				const isVisible = await element.isIntersectingViewport();
				if (isVisible) {
					break;
				}
			}
			const textArray = text.toString().split('');
			await element.click(); // 将焦点放在输入框上
			// await this.page.evaluate((el) => {
			// 	el.scrollIntoView({
			// 		behavior: 'smooth',
			// 		block: 'center',
			// 	});
			// 	const event = new MouseEvent('click', {
			// 		view: window,
			// 		bubbles: true,
			// 		cancelable: true,
			// 	});
			// 	el.dispatchEvent(event);
			// }, element);
			this.page.keyboard.down('Control');
			this.page.keyboard.down('KeyA');
			this.page.keyboard.up('Control');
			this.page.keyboard.up('KeyA');
			await this.page.evaluate((el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
			}, element);
			// console.log(textArray);
			await sleep(1000);
			for (const text of textArray) {
				await sleep(30);
				await element.type(text);
			}
			// this.page.keyboard.press('Enter');
			console.log(`✅找到:${nodeName}.并输入:${text}`);
			await sleep(sleepTime || 1);
			return element;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	// 通过事件派发的方式点击元素输入
	async inputWhileTypeEventBySelector(nodeName, text, selector, timeout, sleepTime) {
		try {
			let element;
			while (true) {
				element = await this.page.waitForSelector(selector, { timeout });
				const isVisible = await element.isIntersectingViewport();
				if (isVisible) {
					break;
				}
			}
			const textArray = text.toString().split('');
			// await element.click(); // 将焦点放在输入框上
			await this.page.evaluate((el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
				const event = new MouseEvent('click', {
					view: window,
					bubbles: true,
					cancelable: true,
				});
				el.dispatchEvent(event);
			}, element);
			this.page.keyboard.down('Control');
			this.page.keyboard.down('KeyA');
			this.page.keyboard.up('Control');
			this.page.keyboard.up('KeyA');
			await this.page.evaluate((el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
			}, element);
			// console.log(textArray);
			await sleep(1000);
			for (const text of textArray) {
				await sleep(30);
				await element.type(text);
			}
			// this.page.keyboard.press('Enter');
			console.log(`✅找到:${nodeName}.并输入:${text}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	// 清空输入框 并直接元素赋值
	async inputClearTextEvaluateBySelector(nodeName, text, selector, timeout) {
		try {
			const element = await this.page.waitForSelector(selector, { timeout });
			// 清空输入框
			await this.page.evaluate(
				(el, value) => {
					el.value = value; // 将值赋给输入框
				},
				element,
				text,
			);
			console.log(`✅找到:${nodeName}.并输入:${text}`);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`);
			return false;
		}
	}

	async clickBySelectorByElement(nodeName, Element, selector, timeout, sleepTime) {
		try {
			const element = await Element.waitForSelector(selector, { timeout });
			await element.click();
			console.log(`✅找到并模拟点击:${nodeName}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	async clickByEvalSelectorByElement(nodeName, Element, selector, timeout, sleepTime) {
		try {
			await Element.waitForSelector(selector, { timeout });
			const element = await this.page.$eval(selector, (el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
				el.click();
			});
			console.log(`✅找到并点击:${nodeName}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	async inputWhileTypeBySelectorByElement(nodeName, el, text, selector, timeout, sleepTime) {
		try {
			const element = await el.waitForSelector(selector, { timeout });
			const textArray = text.toString().split('');
			await element.click(); // 将焦点放在输入框上
			await el.evaluate((el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
				el.setSelectionRange(0, el.value.length);
			}, element);
			await element.press('Backspace'); // 删除选中的文本
			for (const text of textArray) {
				await element.type(text);
			}
			console.log(`✅找到:${nodeName}.并输入:${text}`);
			await sleep(sleepTime || 1);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	async inputClearTextEvaluateBySelectorByElement(nodeName, el, text, selector, timeout) {
		try {
			const element = await el.waitForSelector(selector, { timeout });
			// 清空输入框
			await el.evaluate(
				(el, value) => {
					el.value = value; // 将值赋给输入框
				},
				element,
				text,
			);
			console.log(`✅找到:${nodeName}.并输入:${text}`);
			return true;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	async findArrBySelectorByElement(nodeName, el, selector, timeout, sleepTime) {
		try {
			await el.waitForSelector(selector, { timeout });
			const elements = await el.$$(selector);
			if (elements.length > 0) {
				console.log(`✅找到并点击:${nodeName}`);
				await sleep(sleepTime || 1);
				return elements;
			} else {
				await sleep(sleepTime || 1);
				return false;
			}
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	async findOneBySelectorByElement(nodeName, el, selector, timeout, sleepTime) {
		try {
			const element = await el.$(selector, { timeout });
			if (element) {
				console.log(`✅找到:${nodeName}`);
				await sleep(sleepTime || 1);
				await element.scrollIntoView();
				return element;
			} else {
				console.log(`❌没找到:${nodeName}`);
				return false;
			}
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error.toString());
			return false;
		}
	}
	async findOneBySelectorByElementScroll(nodeName, el, selector, timeout, sleepTime) {
		try {
			const element = await el.$(selector, { timeout });
			console.log(el, element);
			console.log(`✅找到并点击:${nodeName}`);
			await el.evaluate((el) => {
				el.scrollIntoView({
					behavior: 'smooth',
					block: 'center',
				});
			}, element);
			await element.click();
			await sleep(sleepTime || 1);
			return element;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
	async hoverOneBySelector(nodeName, selector, timeout, sleepTime) {
		try {
			const element = await this.page.waitForSelector(selector, { timeout });
			console.log(`✅找到并hover:${nodeName}`);
			await element.hover();
			await sleep(sleepTime || 1);
			return element;
		} catch (error) {
			console.log(`❌没找到:${nodeName}`, error);
			return false;
		}
	}
}

async function sleep(ms) {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

module.exports = yzApi;
